<script setup>
  import { ref,getCurrentInstance,onMounted,reactive} from 'vue'
  import * as echarts from 'echarts'

  const {proxy}=getCurrentInstance()
  const getImageUrl = (user) => {
    return new URL(`../assets/images/${user}.png`, import.meta.url).href
  }
  const tableData = ref([])
  const countData=ref([])
  const chartData=ref([])
  const observer=ref(null)
  const tableLabel = ref({
    name: "课程",
    todayBuy: "今日购买",
    monthBuy: "本月购买",
    totalBuy: "总购买",
  })

  //这个是折线图和柱状图 两个图表共用的公共配置
  //echarts官网
  const xOptions = reactive({
      // 图例文字颜色
      textStyle: {
        color: "#333",
      },
      legend: {},
      grid: {
        left: "20%",
      },
      // 提示框
      tooltip: {
        trigger: "axis",
      },
      xAxis: {
        type: "category", // 类目轴
        data: [],
        axisLine: {
          lineStyle: {
            color: "#17b3a3",
          },
        },
        axisLabel: {
          interval: 0,
          color: "#333",
        },
      },
      yAxis: [
        {
          type: "value",
          axisLine: {
            lineStyle: {
              color: "#17b3a3",
            },
          },
        },
      ],
      color: ["#2ec7c9", "#b6a2de", "#5ab1ef", "#ffb980", "#d87a80", "#8d98b3"],
      series: [],
})
  const pieOptions = reactive({
  tooltip: {
    trigger: "item",
  },
  legend: {},
  color: [
    "#0f78f4",
    "#dd536b",
    "#9462e5",
    "#a6a6a6",
    "#e1bb22",
    "#39c362",
    "#3ed1cf",
  ],
  series: []
})


  // axios({
  //   url:'/api/home/getTableData',
  //   method:'get'
  // }).then(res=>{
  //   console.log(res.data)
  //   //学会制造假数据，把交互的请求的流程 根据接口文档跑通 制造假数据
  //   //拦截请求 我们根据接口文档制造假数据
  //   if(res.data.code===200){
  //     console.log(res.data.data.tableData)
  //     tableData.value=res.data.data.tableData

  //   }
  // })
//将接口返回的数据赋值给响应式变量

const getTableData=async ()=>{
 const data= await proxy.$api.getTableData()
//  console.log(data);
tableData.value=data.tableData
 
}
const getCountData=async ()=>{
 const data= await proxy.$api.getCountData()
//  console.log(data);
 countData.value=data

}
//
const getChartData=async ()=>{
  //获取接口数据
  const {orderData,videoData,userData}= await proxy.$api.getChartData()
  //  console.log(data);
  //处理数据为 ECharts 格式
  xOptions.xAxis.data=orderData.date
  xOptions.series=Object.keys(orderData.data[0]).map(val=>{
    return {
      name:val,
      data:orderData.data.map(item=>item[val]),
      type:"line",
    }
  })
  //初始化并渲染折线图
 const oneEchart=echarts.init(proxy.$refs["echart"])
 oneEchart.setOption(xOptions)
  //渲染柱状图
  xOptions.series=[
    {
      name:"新增用户",
      data:userData.map(item=>item.new),
      type:"bar",
    },
     {
      name:"活跃用户",
      data:userData.map(item=>item.active),
      type:"bar",
    },
  ]
  xOptions.xAxis.data=userData.map(item=>item.date)
  
  const twoEchart=echarts.init(proxy.$refs["userEchart"])
  twoEchart.setOption(xOptions)
  //渲染饼状图
pieOptions.series=[
  {
    data:videoData,
    type: "pie",
    // radius: ["40%", "70%"],
  }
]
  const threeEchart=echarts.init(proxy.$refs["videoEchart"])
  threeEchart.setOption(pieOptions)

  //监听页面的变化
  //如果监听的容器大小发生变化会调用执行函数，调整图表大小
  observer.value=new ResizeObserver((en)=>{
    oneEchart.resize()
    twoEchart.resize()
    threeEchart.resize()
  })
  //容器变化
  if(proxy.$refs["echart"]){
    observer.value.observe(proxy.$refs["echart"])
  }

}


//在组件 DOM 挂载完成后，自动执行 getTableData 方法，
// 触发数据请求，确保页面渲染时能获取到最新数据。初始化数据
onMounted(()=>{
  getTableData()
  getCountData()
  getChartData()
})
</script>

<template>
<el-row class="home" :gutter="20">
  <el-col :span="8" style="margin-top: 20px;">
     <el-card shadow="hover">
      <div class="user">
        <img :src="getImageUrl('user')" class="user" />
        <div class="user-info">
          <p class="user-info-admin">Admin</p>
          <p class="user-info-p">超级管理员</p>         
        </div>
      </div>
      <div class="login-info">
        <p>上次登录时间：<span>2022-01-01 12:00:00</span></p>
        <p>上次登录：地点<span>北京</span></p>
      </div>
     </el-card>
     
     <el-card shadow="hover" class="user-table">
      <el-table :data="tableData">
        <el-table-column 
          v-for="(val,key) in tableLabel"
          :key="key"
          :prop="key"
          :label="val"
        >
        </el-table-column>
      </el-table>
     </el-card>
  </el-col>
  <el-col :span="16" style="margin-top:20px">
    <div class="user">
      <el-card
        :body-style="{display:'flex',padding:0}"
        v-for="item in countData"
        :key="item.name"
      >
        <component :is="item.icon" class="icons" :style="{background:item.color}"></component>
        <div class="detail">
          <p class="num">￥{{ item.value }}</p>
          <p class="txt">￥{{ item.name }}</p>
        </div>
      </el-card>
      <el-card class="top-echart" style="width:100%">
        <div ref="echart" style="height:280px;width:100%">
          
        </div>
      </el-card>
      <!-- 柱状图 -->
      <div class="graph" style="width:100%;height: 260px;">
      <!-- 第一个卡片 -->
      <el-card>
        <div ref="userEchart" style="width:100%; height: 220px;"></div>
      </el-card>
      
      <!-- 第二个卡片 -->
      <el-card>
        <div ref="videoEchart" style="width:100%; height: 220px;"></div>
      </el-card>
</div>
    </div>

  </el-col>
</el-row> 
</template >
<style scoped >
 .home {
  height: 100%;
  overflow: hidden;
 }
 .user {
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ccc;
  margin-bottom: 20px;
  img {
    width: 150px;
    height: 150px;
    border-radius: 50%;
    margin-right: 40px;
  }
  .user-info {
    p{
      line-height: 40px;
    }
    .user-info-p {
      color: #999;
    }
    .user-info-adimin{
      font-size: 35px;
    }
  }
 }
 .login-info {
  p {
    line-height: 30px;
    font-size: 14px;
    color:#999;
    span{
      color:#666;
      margin-left: 60px;

    }
  }
 }
 .user-table {
  margin-top: 20px;
 }
 .user {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
    .el-card {
      width: 32%;
      margin-bottom: 20px;
    }
    
    .icons {
      width: 80px;
      height: 80px;
      font-size: 30px;
      text-align: center;
      line-height: 80px;
      color: #fff;

    }
    .detail {
      margin-left: 15px;
      display: flex;
      flex-direction: column;
      justify-content: center;
      .num {
        font-size: 30px;
        margin-bottom: 20px;      
      }
      .txt {
        font-size: 15px;
        text-align: center;
        color: #999;
      }
    }
   .graph {
    display: flex;
    justify-content: space-between;
    gap: 20px; /* 卡片之间的间距 */
  }

  .graph .el-card {
    width: 48%;  /* 两个卡片各占约一半宽度 */
    height: 260px;
    box-sizing: border-box;
    padding: 10px;
  }
 }
</style>

